package de.mhu.demo;

import java.util.List;

import org.codehaus.jackson.JsonNode;

import de.mhu.mandalaui.Ui;
import de.mhu.mandalaui.Window;
import de.mhu.mandalaui.actions.ClickAction;
import de.mhu.mandalaui.attributes.NamedAttribute;
import de.mhu.mandalaui.bind.DataProvider;
import de.mhu.mandalaui.bind.DataRow;
import de.mhu.mandalaui.bind.SingleDataProvider;
import de.mhu.mandalaui.bind.StaticDataSource;
import de.mhu.mandalaui.bind.StaticSingleDataSource;
import de.mhu.mandalaui.layout.HorizontalLayout;
import de.mhu.mandalaui.layout.Panel;
import de.mhu.mandalaui.layout.VerticalLayout;
import de.mhu.mandalaui.properties.PropertiesTable;
import de.mhu.mandalaui.properties.StringProperty;
import de.mhu.mandalaui.table.Table;
import de.mhu.mandalaui.table.TableHeader;
import de.mhu.mandalaui.table.TableRow;
import de.mhu.mandalaui.text.Text;
import de.mhu.mandalaui.widgets.Button;
import de.mhus.lib.lang.Raw;

public class DemoTableWithStaticDataSource {

	public Window createWindow(String id) {
		Window window = Ui.createWindow(id);
		
		VerticalLayout mainLayout = new VerticalLayout();
		window.setContent(mainLayout);
		
		HorizontalLayout layout = new HorizontalLayout();
		mainLayout.add(layout);
		
		Panel left = new Panel("Left");
		layout.add(left, new HorizontalLayout.Options(3));
		
		Table table = new Table();
		table.setHeader(new TableHeader( new Text("Name") ));
		
		Text c1 = new Text("");
		c1.setBindValue(new NamedAttribute("name"));
		
		table.setRow(new TableRow(c1));
		
//		ClickAction action = new AlertInfoAction();
//		table.setSelectAction(action);
		
		left.add(table);
		
		Panel right = new Panel("Right");
		layout.add(right, new HorizontalLayout.Options(9));

		PropertiesTable properties = new PropertiesTable();
		
		right.add(properties);
		
		properties.add(new StringProperty("firstname","first"), new PropertiesTable.Options("First Name"));
		properties.add(new StringProperty("lastname","last"), new PropertiesTable.Options("Last Name"));

		StaticSingleDataSource ds1 = new StaticSingleDataSource();
		ds1.setTarget(properties.getDataAttribute());
		ds1.setProvider(new SingleDataProvider() {
			
			@Override
			public DataRow getData(JsonNode in) {
				return new DataRow("firstname", "Mickey", "lastname", "Mouse");
			}
		});
		window.getRegistry().add(ds1);
		window.getInitialAction().getActions().add(ds1);

		StaticSingleDataSource ds2 = new StaticSingleDataSource();
		ds2.setTarget(properties.getDataAttribute());
		ds2.setProvider(new SingleDataProvider() {
			
			@Override
			public DataRow getData(JsonNode in) {
				return new DataRow("firstname", "Donald", "lastname", new Raw("JSON.stringify(data)"));
			}
		});

		StaticDataSource ds = new StaticDataSource();
		window.getRegistry().add(ds);
		ds.setTarget(table.getDataAttribute());
		ds.setProvider(new DataProvider() {
			
			@Override
			public void fillWithData(JsonNode in, List<DataRow> data) {
				data.add(new DataRow("name", "Hans"));
			}
		});
		window.getInitialAction().getActions().add(ds);
		
		Button button = new Button("Click");
		mainLayout.add(button);
		button.setClickAction(new ClickAction(ds2, false, table.getDataAttribute() ));
		
		return window;
	}
}
